package adapter.order_adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.text.DecimalFormat;

import activity.order.OrderDeilActivity;
import bean.order_bean.WeixiuKeJieBen;
import util.utls.BiaduMaps;
import util.utls.OrderBtnSed;

/**
 * Created by itboye on 2016/12/6.
 */

public class OrderAdapter extends BaseAdapter implements View.OnClickListener {
    Context Context;
    WeixiuKeJieBen bean;
    OrderBtnSed send;
    double lng;
    double lat;

    public OrderAdapter(Context Context, WeixiuKeJieBen bean, double lng, double lat, OrderBtnSed send) {
        this.Context = Context;
        this.bean = bean;
        this.lng = lng;
        this.lat = lat;
        this.send = send;
    }

    @Override
    public int getCount() {
        return bean.getCount();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHouder houder;
        if (convertView == null) {
            houder = new ViewHouder();
            convertView = LayoutInflater.from(Context).inflate(R.layout.item_orders, null);
            houder.tvNumSize = (TextView) convertView.findViewById(R.id.tvmmSizes);
            houder.tvMatter = (TextView) convertView.findViewById(R.id.tvMatter);
            houder.tvaddress = (TextView) convertView.findViewById(R.id.tvaddress);
            houder.btnSend = (Button) convertView.findViewById(R.id.btnSend);
            convertView.setTag(houder);
        } else {
            houder = (ViewHouder) convertView.getTag();
        }
        double lngt = bean.getList().get(position).getLng();
        double lnat = bean.getList().get(position).getLat();
        double distance = BiaduMaps.GPSLng(lngt, lnat, lng, lat);

        Log.d("lnglng", lng + "为敌" + lat);


        double naece = distance / 1000;
        double b = Double.valueOf(naece);
        DecimalFormat df = new DecimalFormat("######0.00");
        houder.tvNumSize.setText(df.format(b) + "km");


        houder.tvaddress.setText(bean.getList().get(position).getAddress());
        houder.tvMatter.setText(bean.getList().get(position).getRepair_name() + "+" + bean.getList().get(position).getVehicle_name());
        houder.btnSend.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Context, OrderDeilActivity.class);
                intent.putExtra("addres", bean.getList().get(position).getAddress());
                intent.putExtra("repairName", bean.getList().get(position).getRepair_type());
                intent.putExtra("detail", bean.getList().get(position).getDetail());
                intent.putExtra("lng", bean.getList().get(position).getLng());
                intent.putExtra("lat", bean.getList().get(position).getLat());
                intent.putExtra("data", bean.getList().get(position).getCreate_time());
                intent.putExtra("phone", bean.getList().get(position).getMobile());
                intent.putExtra("id", bean.getList().get(position).getId());
                Context.startActivity(intent);
            }
        });
        return convertView;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnSend:
                send.onBtnSend(v);
                break;
        }
    }

    public class ViewHouder {
        TextView tvNumSize, tvMatter, tvaddress;
        Button btnSend;

    }


}
